package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.data.ResponseStausEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.TempLateService;
import com.dongdongshop.util.PageBean;
import com.dongdongshop.util.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @ClassName ItemCatController
 * @Author RC
 * @date 2020.12.11 15:39
 */
@Controller
@RequestMapping("itemcat")
public class ItemCatController {
    @Reference(timeout = 100000,check = false)
    private ItemCatService itemCatService;
    @Reference(timeout = 100000,check = false)
    private TempLateService tempLateService;

    private static final Logger logger = LoggerFactory.getLogger(BrandController.class);

    @RequestMapping("getListItemCat")
    @ResponseBody
    public DataResult getItemCatList(@RequestParam(defaultValue = "1")Integer pageNumber, @RequestParam(defaultValue = "5")Integer pageSize,@RequestParam(defaultValue = "0") Long parentId) {
        try {
            logger.info("进入查询分类方法");
            PageUtil<TbItemCat> pageUtil = new PageUtil<>();
            pageUtil.setPageNumber(pageNumber);
            pageUtil.setPageSize(pageSize);
            Integer startPage  = pageUtil.getStartPage();
            List<TbItemCat> tbTypeTemplates = itemCatService.getListItemCat(startPage,pageSize,parentId);
            pageUtil.setList(tbTypeTemplates);
            Integer total = itemCatService.selectCount(parentId);
            pageUtil.setTotal(total);
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(pageUtil);


        } catch (Exception e) {
            logger.info("查询失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("saveTtemcat")
    @ResponseBody
    public DataResult saveTtemcat(TbItemCat tbItemCat) {
        try {
            logger.info("进入保存分类方法");
            itemCatService.saveTtemcat(tbItemCat);
            return DataResult.response(ResponseStausEnum.SUSSCESS);/*.setData();*/


        } catch (Exception e) {
            logger.info("保存失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("toUpdateItemCat/{id}")
    @ResponseBody
    public DataResult toUpdateItemCat(@PathVariable("id") Long id) {
        try {
            logger.info("进入保存分类方法");
            TbItemCat tbItemCat = itemCatService.getItemCatById(id);
            Long typeId = tbItemCat.getTypeId();
            TbTypeTemplate tbTypeTemplates = tempLateService.getTempLateByTypeId(typeId);
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("tbItemCat",tbItemCat);
            map.put("typeTemp",tbTypeTemplates);
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(map);
        } catch (Exception e) {
            logger.info("保存失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateItem")
    @ResponseBody
    public DataResult updateItem(TbItemCat tbItemCat) {
        try {
            logger.info("进入修改分类方法");
            itemCatService.updateItem(tbItemCat);

            return DataResult.response(ResponseStausEnum.SUSSCESS);
        } catch (Exception e) {
            logger.info("修改失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("deleteBatchCat/{ids}")
    @ResponseBody
    public DataResult deleteBatchCat(@PathVariable("ids")Integer[] ids) {
        try {
            logger.info("进入修改分类方法");
            itemCatService.deleteBatchCat(ids);

            return DataResult.response(ResponseStausEnum.SUSSCESS);
        } catch (Exception e) {
            logger.info("修改失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }


}
